


package yf.mall.controller;
import com.yf.request.SearchParam;

import java.util.List;

import com.yf.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.dao.MallGoodsDao;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.service.MallGoodsService;
import yf.mall.service.SysUsersService;
import yf.mall.vo.QuestionVo;
import yf.mall.service.QuestionService;
import com.yf.response.R;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
                                        import yf.mall.enums.QuestionStatusEnum;
    
/**
 * 问题表Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Dec 15 13:15:22 CST 2020
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private SysUsersService sysUsersService;
    @Autowired
    private MallGoodsDao goodsDao;

    /**
     * 列表问题表
     */
    @SysLog("查看问题表")
    @RequestMapping("/list")
    @RequiresPermissions("question:list")
    public R list(@RequestBody SearchParam<QuestionVo> params) {
        Long sys_userId = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sys_userId);
        if (userId !=null){
            params.getEntity().setUserId(userId);
        }
        List<MallGoodsEntity> goodsEntities = goodsDao.findByUserId(userId,null);
        PageHelper.startPage(params.getPage(), params.getLimit());
        List<QuestionVo> list = questionService.findVo(params.getEntity());
        return R.ok(new PageInfo(list)).putExtra("userId",userId).putExtra("goodsList",goodsEntities);
    }

    /**
 * 下载订单
 */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("question:list")
    public void down(@RequestBody SearchParam<QuestionVo> params, HttpServletResponse response) {
        List<QuestionVo> list = questionService.findVo(params.getEntity());
            questionService.downLoad(response, list);
    }


    /**
     * 查看问题表
     */
    @SysLog("查看问题表")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("question:info")
    public R info(@PathVariable("id") Long id) {
            QuestionVo vo = questionService.getVo(id);

        return R.ok(vo);
    }

    /**
     * 保存问题表
     */
    @SysLog("保存问题表")
    @RequestMapping("/save")
    @RequiresPermissions("question:save")
    public R save(@RequestBody QuestionVo vo) {
            questionService.saveVo(vo);
        return R.ok(vo);
    }

    /**
     * 修改问题表
     */
    @SysLog("修改问题表")
    @RequestMapping("/update")
    @RequiresPermissions("question:update")
    public R update(@RequestBody QuestionVo vo) {
            questionService.updateVo(vo);

        return R.ok(vo);
    }

    /**
     * 删除问题表
     */
    @SysLog("删除问题表")
    @RequestMapping("/delete")
    @RequiresPermissions("question:delete")
    public R delete(Long[]ids) {
        questionService.del(ids);
        return R.ok();
    }

    /**
     * 所有问题表
     */
    @SysLog("所有问题表")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order!=null ? order : "id desc");
        List<QuestionVo> list = questionService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String,Object> options = new HashMap();
                                                                                                                                                                                options.put("statusOptions",QuestionStatusEnum.names());
                            return R.ok(options);
    }





    /***********自定义开始***********/
}
